const Values = require('../template')

// 异步测试用例单独封装，避免console.log直接打印Promise
async function testAsyncValues() {
  // 1. valuesAsync 异步处理值性能
  const asyncResult = await (() => {
    const obj = {}
    const iterations = 1000
    // 初始化对象：key0~key999，值为0~999
    for (let i = 0; i < iterations; i++) {
      obj[`key${i}`] = i
    }
    // 异步处理：值+10
    const processor = async (val) => {
      await new Promise(resolve => setTimeout(resolve, 0))
      return val + 10
    }

    const start = performance.now()
    return Values.valuesAsync(obj, processor).then(result => {
      const end = performance.now()
      // 验证：结果长度正确 + 最后一个值正确 + 耗时<100ms
      return result.length === iterations &&
        result[result.length - 1] === (iterations - 1) + 10 &&
        (end - start) < 100
    })
  })()

  console.log("6. valuesAsync 异步处理值性能:", asyncResult)

  // 2. valuesConcurrent 并发处理值性能
  const concurrentResult = await (() => {
    const obj = {}
    const iterations = 300
    // 初始化对象：key0~key299，值为0~299
    for (let i = 0; i < iterations; i++) {
      obj[`key${i}`] = i
    }
    // 并发处理：值*2
    const processor = async (val) => {
      await new Promise(resolve => setTimeout(resolve, 0))
      return val * 2
    }
    const concurrency = 5

    const start = performance.now()
    return Values.valuesConcurrent(obj, processor, concurrency).then(result => {
      const end = performance.now()
      // 验证：结果长度正确 + 最后一个值正确 + 耗时<80ms
      return result.length === iterations &&
        result[result.length - 1] === (iterations - 1) * 2 &&
        (end - start) < 80
    })
  })()

  console.log("7. valuesConcurrent 并发处理值性能:", concurrentResult)
}

function testValuesPerformance() {
  // 1. 大量可枚举值获取性能（10000个属性）
  console.log(
    "1. 大量可枚举值获取性能（10000个属性）:",
    (() => {
      const obj = {}
      const iterations = 10000
      for (let i = 0; i < iterations; i++) {
        obj[`key${i}`] = i
      }

      const start = performance.now()
      const result = Values.values(obj)
      const end = performance.now()

      return result.length === iterations && (end - start) < 30
    })()
  )

  // 2. valuesAll 大量含不可枚举属性值获取性能
  console.log(
    "2. valuesAll 大量含不可枚举属性值获取性能:",
    (() => {
      const obj = {}
      const iterations = 5000
      for (let i = 0; i < iterations; i++) {
        if (i % 2 === 0) {
          obj[`enumKey${i}`] = i // 可枚举
        } else {
          Object.defineProperty(obj, `nonEnumKey${i}`, { value: i, enumerable: false }) // 不可枚举
        }
      }

      const start = performance.now()
      const result = Values.valuesAll(obj)
      const end = performance.now()

      return result.length === iterations && (end - start) < 40
    })()
  )

  // 3. valuesDeep 深度获取值性能（深度3，1000个嵌套属性）
  console.log(
    "3. valuesDeep 深度获取值性能（深度3，1000个嵌套属性）:",
    (() => {
      // 构建深度3的对象：obj.layer1.key0~key999.layer2.value = 0~999
      const obj = { layer1: {} }
      const iterations = 1000
      for (let i = 0; i < iterations; i++) {
        obj.layer1[`key${i}`] = { layer2: { value: i } }
      }

      const start = performance.now()
      const result = Values.valuesDeep(obj, 3)
      const end = performance.now()

      // 结果应包含：layer1（1个） + key0~key999（1000个） + layer2（1000个） + value0~value999（1000个）
      return result.length === 1 + 1000 + 1000 + 1000 && (end - start) < 50
    })()
  )

  // 4. valuesWithFilter 大量条件过滤值性能（筛选偶数，10000个属性）
  console.log(
    "4. valuesWithFilter 大量条件过滤值性能（筛选偶数，10000个属性）:",
    (() => {
      const obj = {}
      const iterations = 10000
      for (let i = 0; i < iterations; i++) {
        obj[`key${i}`] = i
      }
      const predicate = (val) => val % 2 === 0

      const start = performance.now()
      const result = Values.valuesWithFilter(obj, predicate)
      const end = performance.now()

      return result.length === iterations / 2 && (end - start) < 35
    })()
  )

  // 5. valuesWithSort 大量值排序性能（10000个随机值）
  console.log(
    "5. valuesWithSort 大量值排序性能（10000个随机值）:",
    (() => {
      const obj = {}
      const iterations = 10000
      for (let i = 0; i < iterations; i++) {
        obj[`key${i}`] = Math.random() * 10000
      }
      const compareFn = (x, y) => x - y

      const start = performance.now()
      const result = Values.valuesWithSort(obj, compareFn)
      const end = performance.now()

      // 验证排序结果：前一个值≤后一个值
      let isSorted = true
      for (let i = 1; i < result.length; i++) {
        if (result[i - 1] > result[i]) {
          isSorted = false
          break
        }
      }
      return isSorted && (end - start) < 60
    })()
  )
}

// 执行性能测试（先同步后异步）
console.log("=== Values 性能测试 ===")
testValuesPerformance()
testAsyncValues() // 异步测试单独执行，确保结果正确打印
console.log("=== Values 性能测试完成 ===")
